package com.example.photography.Activity.Edit;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.photography.Activity.Photo_Publish;
import com.example.photography.Activity.Product_Content;
import com.example.photography.R;
import com.example.photography.bean.Product_bean;
import com.example.photography.bean.User_Info;
import com.example.photography.common.BaseActivity;
import com.example.photography.common.Constant;
import com.example.photography.retrofit.ApiService;
import com.example.photography.retrofit.net.exception.ApiException;
import com.example.photography.retrofit.net.exception.ErrorConsumer;
import com.example.photography.retrofit.net.response.ResponseTransformer;
import com.example.photography.util.Util;
import com.example.photography.view.LoadingDialog;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.functions.Consumer;
@AndroidEntryPoint
public class Edit_sex extends BaseActivity {

    @BindView(R.id.iv_back)
    TextView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.iv_men_ture)
    ImageView ivMenTure;
    @BindView(R.id.ll_men)
    LinearLayout llMen;
    @BindView(R.id.iv_woman_ture)
    ImageView ivWomanTure;
    @BindView(R.id.ll_woman)
    LinearLayout llWoman;
    private boolean ismen = true;
    //注入动态代理实例对象
    @Inject
    ApiService apiService;
    @Override
    protected void initData() {
        tvTitle.setText("修改性别");
        llMen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ivMenTure.setVisibility(View.VISIBLE);
                ivWomanTure.setVisibility(View.GONE);
                ismen = true;
            }
        });

        llWoman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ivMenTure.setVisibility(View.GONE);
                ivWomanTure.setVisibility(View.VISIBLE);
                ismen = false;
            }
        });
        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                retrofit();
            }
        });
    }

    @SuppressLint("CheckResult")
    private void retrofit() {
        //开启等待动画
        LoadingDialog.getInstance(this).show();
        Map<String,String> params = new HashMap<>();
        params.put("id_unique", Constant.UID);
        params.put("sex",ismen?"0":"1");
        apiService.user_edit(params)
                .compose(ResponseTransformer.obtain())
                .subscribe(new Consumer<User_Info>() {
                    @Override
                    public void accept(User_Info userInfo) throws Exception {
                        //正常情况下处理数据回调
                        LoadingDialog.getInstance(Edit_sex.this).dismiss();
                        Util.showToast(Edit_sex.this, "修改成功~");
                        removeCurrentActivity();
                    }
                }, new ErrorConsumer() {
                    @Override
                    protected void error(ApiException e) {
                        Log.i("test1", "e=" + e.getErrorMsg());
                        LoadingDialog.getInstance(Edit_sex.this).dismiss();
                        Util.showToast(getApplicationContext(), "修改失败："+e.getErrorMsg());
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_sex;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
